/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.fxn.component;

import com.futuremind.recyclerviewfastscroll.ListContainerScrolledListener;
import com.futuremind.recyclerviewfastscroll.viewprovider.DefaultBubbleBehavior;
import com.futuremind.recyclerviewfastscroll.viewprovider.ScrollerViewProvider;
import com.futuremind.recyclerviewfastscroll.viewprovider.ViewBehavior;
import com.futuremind.recyclerviewfastscroll.viewprovider.VisibilityAnimationManager;
import com.fxn.pix.ResourceTable;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.global.resource.NotExistException;

import java.io.IOException;

/**
 * description : ScrollerViewProvider 的子类
 *
 * @since 2021-04-15
 */
public class CustomScrollerViewProvider extends ScrollerViewProvider {
    private Text bubble;
    private Component handle;

    @Override
    public Component provideHandleView(ComponentContainer container) {
        handle = new Component(getContext());
        int width = AttrHelper.vp2px(40, getContext());
        int height = AttrHelper.vp2px(50, getContext());
        handle.setLayoutConfig(new ComponentContainer.LayoutConfig(width, height));
        ShapeElement element = (ShapeElement) ElementScatter
                .getInstance(getContext()).parse(ResourceTable.Graphic_trigger);
        float left = AttrHelper.vp2px(25, getContext());
        float[] radios = {left, left, 0, 0, 0, 0, left, left};
        element.setCornerRadiiArray(radios);
        try {
            PixelMapElement foreground = null;
            foreground = new PixelMapElement(getContext().getResourceManager().getResource(ResourceTable.Media_handle));
            handle.setForeground(foreground);
        } catch (IOException | NotExistException e) {
            e.printStackTrace();
        }
        handle.setBackground(element);
        handle.setVisibility(Component.INVISIBLE);
        return handle;
    }

    @Override
    public Component provideBubbleView(ComponentContainer container) {
        bubble = new Text(getContext());
        int width = AttrHelper.vp2px(140, getContext());
        int height = AttrHelper.vp2px(30, getContext());
        bubble.setLayoutConfig(new ComponentContainer.LayoutConfig(width, height));
        if (ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_trigger) instanceof ShapeElement) {
            ShapeElement element = (ShapeElement) ElementScatter.getInstance(getContext())
                    .parse(ResourceTable.Graphic_trigger);
            element.setCornerRadius(AttrHelper.vp2px(15, getContext()));
            bubble.setBackground(element);
        }
        bubble.setVisibility(Component.INVISIBLE);
        bubble.setTextAlignment(TextAlignment.CENTER);
        bubble.setTextSize(14, Text.TextSizeType.FP);
        bubble.setTextColor(new Color(Color.getIntColor("#000000")));
        getScroller().addScrolledListener(new ListContainerScrolledListener.ScrolledListener() {
            @Override
            public void onScroll(float relativePos) {
            }
        });
        return bubble;
    }

    @Override
    public Text provideBubbleTextView() {
        return bubble;
    }

    @Override
    public int getBubbleOffset() {
        return (int) (getScroller().isVertical() ?
                (double) handle.getHeight() / 2f - (double) bubble.getHeight() / 2f :
                (double) handle.getWidth() / 2f - (double) bubble.getWidth() / 2);
    }

    @Override
    protected ViewBehavior provideHandleBehavior() {
        return new CustomHandleBehavior(
                new VisibilityAnimationManager.Builder(handle)
                        .withHideDelay(2000)
                        .build(),

                new CustomHandleBehavior.HandleAnimationManager.Builder(handle)
                        .build()
        );
    }

    @Override
    protected ViewBehavior provideBubbleBehavior() {
        return new DefaultBubbleBehavior(new VisibilityAnimationManager.Builder(bubble).withHideDelay(0).build());
    }
}
